package cn.HuanG.order.service.impl;

import cn.HuanG.base.constant.PayBillConstants;
import cn.HuanG.base.service.impl.BaseServiceImpl;
import cn.HuanG.order.domain.OrderAddress;
import cn.HuanG.order.domain.OrderAdopt;
import cn.HuanG.order.mapper.OrderAddressMapper;
import cn.HuanG.order.service.IOrderAdoptService;
import cn.HuanG.pay.domain.AlipayInfo;
import cn.HuanG.pay.domain.PayBill;
import cn.HuanG.pay.mapper.AlipayInfoMapper;
import cn.HuanG.pay.mapper.PayBillMapper;
import cn.HuanG.pay.service.IPayService;
import cn.HuanG.pet.domain.Pet;
import cn.HuanG.pet.mapper.PetMapper;
import cn.HuanG.quartz.domain.QuartzJobInfo;
import cn.HuanG.quartz.service.IQuartzService;
import cn.HuanG.user.domain.User;
import cn.HuanG.user.domain.UserAddress;
import cn.HuanG.user.mapper.UserAddressMapper;
import cn.HuanG.util.CodeGenerateUtils;
import cn.HuanG.util.LoginContext;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@Service
@Transactional
public class OrderAdoptServiceImpl  extends BaseServiceImpl<OrderAdopt> implements IOrderAdoptService {
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private UserAddressMapper userAddressMapper;
    @Autowired
    private OrderAddressMapper orderAddressMapper;
    @Autowired
    private PayBillMapper payBillMapper;
    @Autowired
    private AlipayInfoMapper alipayInfoMapper;
    @Autowired
    private IPayService iPayService;

    @Autowired
    private IQuartzService quartzService;

    @Override
    public String createOrder(Map<String, String> map, HttpServletRequest request) {
        /*
         * 	pet_id:this.pet.id,//领养宠物
         * 	address_id:1,//要配送的地址
         * 	payMethod:'ALIPAY'//支付方式
         */
        //获取前台的数据pet_id
        Long pet_id = Long.valueOf(map.get("pet_id"));
        //获取前台数据address_id
        Long address_id = Long.valueOf(map.get("address_id"));
        System.out.println("地址"+address_id);
        String ALIPAY = map.get("payMethod");
        //1、根据用户选择的配送地址，查询出userAddress
        UserAddress userAddress = userAddressMapper.queryById(address_id);

//        2、根据领养的宠物id  pet对象查询出来
        Pet pet = petMapper.queryById(pet_id);
//        3、根据pet对象  生成一个OrderAdopt保存
        OrderAdopt orderAdopt = pet2orderAdopet(pet,request);
        //保存orderAdopt  返回主键
        super.save(orderAdopt);//返回主键
//        4、根据用户的地址  生成一个OrderAddress保存
       OrderAddress orderAddress =  userAddress2orderAdress(userAddress);
      //需要维护的sn
        orderAddress.setOrderSn(orderAdopt.getOrderSn());
        //需要维护adopt_id
        orderAddress.setOrder_id(orderAddress.getId());
        //保存数据并返回主键,因为在上面保存adopt时没有address，所以在address保存后，有了主键，才能保存在adopt表中
        orderAddressMapper.save(orderAddress);
        //更新数据库，将新生成的address的主键修改更新到address的数据库里
        orderAdopt.setAddress_id(orderAddress.getId());
        super.update(orderAdopt);
//        5、生成一个支付订单
        PayBill payBill = orderAdopt2payBill(orderAdopt);
        payBill.setPayChannel(ALIPAY);
        payBill.setMoney(orderAdopt.getPrice());
        payBill.setCreateTime(new Date());
        payBillMapper.save(payBill);

//        6、使用支付宝支付，把值传回去
        AlipayInfo alipayInfo = alipayInfoMapper.queryByShopId(orderAdopt.getShop_id());
        String result = iPayService.pay(payBill, alipayInfo);

        //7、增加对应的定时任务
        Map<String, Object> paramsMap = new HashMap<>();
        System.out.println("订单号"+payBill.getOrderSn());
        paramsMap.put("orderSn",payBill.getOrderSn());
        //设置时间表达式，调用方法
        QuartzJobInfo info = new QuartzJobInfo();
        info.setJobName(payBill.getOrderSn());
        info.setParams(paramsMap);
        info.setFireDate(payBill.getLastPayTime());

        quartzService.addJob(info);
        //将result返回到前台
        return result;
    }
    private PayBill orderAdopt2payBill(OrderAdopt orderAdopt) {
        PayBill payBill = new PayBill();
        //因两个表数据大致相同直接copy
        BeanUtils.copyProperties(orderAdopt,payBill);
        //需要设置：setBusinessKey,setBusinessType,setPayChannel(支付方式)在上面,setCreateTime 时间,setMoney  钱
        payBill.setBusinessKey(orderAdopt.getId());
        payBill.setBusinessType(PayBillConstants.ORDER_ADOPT);
        return payBill;
    }

    private OrderAddress userAddress2orderAdress(UserAddress userAddress) {
        OrderAddress orderAddress= new OrderAddress();
        //使用方法copy参数第一位是数据源，第二位是目标
        BeanUtils.copyProperties(userAddress,orderAddress);
        return  orderAddress;
    }

    private OrderAdopt pet2orderAdopet(Pet pet, HttpServletRequest request) {
        OrderAdopt orderAdopt = new OrderAdopt();
//        private String digest;  订单名称
        orderAdopt.setDigest("【宠物窝窝】-提交订单");
//        private Integer state;  订单状态
        orderAdopt.setState(0);
        //        private Long user_id;
        User current = (User)LoginContext.getCurrent(request);
        orderAdopt.setUser_id(current.getId());
//        private BigDecimal price; 售价
        orderAdopt.setPrice(pet.getSaleprice());
//        private String orderSn;
        //调用工具类传入登录用户的id
        String s = CodeGenerateUtils.generateOrderSn(current.getId());
        orderAdopt.setOrderSn(s);
//        private String paySn;
//        private Date lastPayTime; 最后支付时间
        orderAdopt.setLastPayTime(new Date(System.currentTimeMillis()+1000*60*1));
//        private Date lastConfirmTime;
//        private Long pet_id;
        orderAdopt.setPet_id(pet.getId());
//        private Long shop_id;
        orderAdopt.setShop_id(pet.getShop_id());
//        private Long address_id;
        return orderAdopt;
    }


}
